package com.neuedu.dao;



import com.neuedu.entity.User;
import com.neuedu.utils.DBUtils;

import java.util.List;
import java.util.Map;

public class UserDao {

    /**
     * 根据条件查询集合
     * @param where
     * @param params
     * @return
     */
    //where  =      where sss = ?
    public List<User> selectList(String where , Object ... params){
        String sql =" select * from user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        System.out.println("sql = " + sql);
        List<User> users = DBUtils.selectList(User.class, sql, params);

        return users;
    }

    /**
     * 根据条件查询一个
     * @param where
     * @param params
     * @return
     */
    public User selectOne(String where ,Object ... params){
        String sql =" select * from user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(User.class, sql, params);
    }


    /**
     * 查询总条数
     * @param where
     * @param params
     * @return
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from user ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }

    /**
     * 根据数据进行插入
     * @param user
     * @return
     */
    public int insert(User user){
        String sql = "INSERT INTO  `user` (  `userid`, `username`, `password`, `expire`, `nickname`, `lastlogin`,  `sal`) VALUES (?,?,?,?,?,?,?)";
        return DBUtils.update( sql,
                user.getUserid(),
                user.getUsername(),
                user.getPassword(),
                user.getExpire(),
                user.getNickname(),
                user.getLastlogin(),
                user.getSal()
                );


    }



    /**
     * 根据主键更新一条
     * @param user
     * @return
     */
    public int updateById(User user){
        StringBuilder sql = new StringBuilder();
        sql.append("  UPDATE 						");
        sql.append("     `user`                     ");
        sql.append("  SET                           ");
        sql.append("                                ");
        sql.append("    `username` = ?,    ");
        sql.append("    `password` = ?,    ");
        sql.append("    `expire` =?,    ");
        sql.append("    `nickname` = ?,    ");
        sql.append("    `lastlogin` = ?,    ");
        sql.append("    `sal` = ?    WHERE `userid` = ?   ");



        return DBUtils.update( sql.toString(),

                user.getUsername(),
                user.getPassword(),
                user.getExpire(),
                user.getNickname(),
                user.getLastlogin(),
                user.getSal(),
                user.getUserid()
        );

    }

    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from user  where userid = ?";
        return DBUtils.update( sql, id);

    }


    /**
     * 根据条件删除
     * @param where
     * @param params
     * @return
     */
    public int delete(String where ,Object... params){
        String sql = " delete from user ";
        if(where == null || "".equals(where)){
            throw new RuntimeException("必须有条件删除");
        }
        sql += where;
        return DBUtils.update( sql, params);

    }

}
